<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\WebController;
use Illuminate\Http\Request;
use Aitifen\Library\Xxtea\Xxtea;
use Illuminate\Support\Facades\Config;

class TeachersController extends WebController
{
    /**
     *  设置教师登录
     */
    public function setTeacherToken(Request $request)
    {
        $token    = trim($request->post('token'));
        $platform = trim($request->post('platform'));

        try {
            $server = $this->getClient(STEAM_SERVICE_NAME,'AuthService');
            $server->clearHeaders();
            $data = $server->setUserToken(['token' => $token,'platformName' => $platform]);
            setcookie('STEAM-ACCESS-TOKEN',$data['accessToken'],time() + 86400,'/');

        } catch (\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }

        return $this->returnJson(0,'登录成功', ['accessToken'=>$data['accessToken']]);
    }

    /**
     *  获取教师详情
     */
    public function info(Request $request)
    {
        try {
            $server = $this->getClient(STEAM_SERVICE_NAME,'AuthService');
            $teacherInfo = $server->getTeacherInfo([]);
        } catch (\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }
        return $this->returnJson(0, '操作成功', $teacherInfo);
    }

    /**
     *  修改教师信息
     */
    public function save(Request $request)
    {
        $name      = trim($request->post('name'));
        $gender    = intval($request->post('gender'));
        $headerUrl = trim($request->post('head_url'));

        $postData = [
            'name'       => $name,
            'gender'     => $gender,
            'head_url'   => $headerUrl,
        ];

        try {
            $server = $this->getClient(STEAM_SERVICE_NAME,'AuthService');
            $bool   = $server->editTeacherInfo($postData);
        } catch (\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }
        return $this->returnJson(0, '操作成功', $bool);
    }

}